<?php
namespace app\a\controller;
use app\BaseController;
use think\facade\Db;
use qiniu\Qn;
class Goods extends Base{

    //后台获取 产品 数据列表
    public function get_list_data_a(){
        $param = input('');

        $search_type = $param['type'] ? ['type' => $param['type']]:[];//判断是否需要筛选 分类
        $search = $param['keyWord'] ? [['productName', 'like', '%'.$param['keyWord'].'%']]:[];//判断是否需要筛选 关键词
        $search_sort = $param['prop'] ? [$param['prop'] => $param['sort']]:[];//排序

        $list = Db::table('an_goods')
        ->order($search_sort)
        ->page($param['page'],$param['limit'])
        ->where($search_type)
        ->where($search)
        ->withoutField(['content'])
        ->select();

        $this->info['data'] = [
            'list' => $list,
            'type' => Db::table('an_type')->where('type', 'goods')->order('id', 'desc')->select()->toArray()
        ];
        $this->info['total'] = Db::table('an_goods')->where($search_type)->where($search)->count();
        return json($this->info);
    }    

    //后台获取 订单 数据列表
    // public function ajax_user_data_a(){
    //     $data = input(''); 
    //     if($data['admin_id'] == 1){
    //         $info['user'] = db('goods_user')->order('id desc')->select();
    //         $info['order'] = db('goods_order')->order('id desc')->select();
    //         $info['admin'] = db('goods_admin')->order('id desc')->select();
    //     }else{
    //         $info['user'] = db('goods_user')->where('admin_id',$data['admin_id'])->order('id desc')->select();
    //         $info['order'] = db('goods_order')->order('id desc')->select();
    //         $info['admin'] = db('goods_admin')->where('id',$data['admin_id'])->order('id desc')->select();
    //     }
    //     return json($info);
    // }

    //获取需要编辑数据
    public function get_detail_a(){
        $param = input('');
        $details = null;
        if($param['id']){
            $details = Db::table('an_goods')->where('id',$param['id'])->find();
        }
        $this->info['data'] = [
            'details'=>$details,
            'type' => Db::table('an_type')->cache('an_type')->where('type', 'goods')->order('id', 'desc')->select()->toArray()
        ];
        return json($this->info);
    }

    //删除数据
    public function del_data_a(){
        $param = input('');
        foreach($param['ids'] as $v){
            Db::table('an_goods')->where('id',$v)->delete();
        }
        $this->logData('删除产品，id为'.implode(",", $param['ids']),$param['token']);
        return json($this->info);
    }

    //更新数据
    public function updata_a(){
        $param = input('');
        Db::table('an_goods')->where('id',$param['id'])->update([
            'up_time'=>time(),
            'productName'=>$param['productName'],
            'productImageList'=>$param['productImageList'],
            'productImage'=>$param['productImage'],
            'productPrice'=>isset($param['productPrice'])?$param['productPrice']:0,
            'content'=>$param['content'],
            'productStock'=>isset($param['productStock'])?$param['productStock']:0,
            'type'=>$param['type'],
            "a_show"=>trim($param['a_show']),
            'sort'=>isset($param['sort'])?$param['sort']:0,
        ]);
        $this->info['msg']='更新成功';
        $this->logData('更新产品，id为'.$param['id'],$param['token']);
        return json($this->info);
    } 

    //新增数据
    public function add_data_a(){
        $param = input('');
        Db::table('an_goods')->insert([
            'create_time'=>time(),
            'up_time'=>time(),
            'productName'=>$param['productName'],
            'productImageList'=>$param['productImageList'],
            'productImage'=>$param['productImage'],
            'productPrice'=>isset($param['productPrice'])?$param['productPrice']:0,
            'productStock'=>isset($param['productStock'])?$param['productStock']:0,
            'content'=>$param['content'],
            'type'=>$param['type'],
            "a_show"=>trim($param['a_show']),
            'sort'=>isset($param['sort'])?$param['sort']:0,
        ]);
        $this->info['msg']='新增成功';
        $this->logData('新增产品，名称为'.$param['productName'],$param['token']);
        return json($this->info);
    }    


    //产品批量上下架
    public function switch_state_data_a(){
        $param = input('');
        foreach($param['ids'] as $v){
            Db::table('an_goods')->where('id',$v)->update(['down_goods'=>$param['state']]);
        }
        $str = $param['state']==1?'下架':'上架';
        $this->logData($str.'产品，id为'.implode(",", $param['ids']),$param['token']);
        return json($this->info);
    }

    //批量修改主图
    // public function upImg(){
    //     $list = Db::table('an_goods')->select()->toArray(); 
    //     foreach($list as $k=>$v){
    //         $imgs = explode(',',$list[$k]['productImageList']);
    //         Db::table('an_goods')->where('id', $list[$k]['id'])->data(['productImage'=>$imgs[0]])->update();
    //     }
    // }
   
    //批量将时间改为时间戳
    // public function upTime(){
    //     $list = Db::table('an_goods')->select()->toArray(); 
    //     foreach($list as $k=>$v){
    //          Db::table('an_goods')->where('id', $list[$k]['id'])->data(['up_time'=>strtotime($list[$k]['up_time'])])->update();
    //     }
    // }
}
